package com.frxuexi.park2303.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.NoticeBeanItem
import com.frxuexi.base.mvi.BaseFragment
import com.frxuexi.base.mvi.UiState
import com.frxuexi.base.utils.ArouterManager
import com.frxuexi.park2303.R
import com.frxuexi.park2303.databinding.FragmentHomeBinding
import com.frxuexi.park2303.notice.NoticeAdapter
import com.frxuexi.park2303.notice.NoticeIntent
import com.frxuexi.park2303.notice.NoticeViewModel
import com.frxuexi.park2303.notice.OnItemClickListener
import com.frxuexi.park2303.notice.OnItemLongClickListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding,NoticeIntent,NoticeViewModel>() {
    val noticeAdapter by lazy { NoticeAdapter() }
    override fun getVM(): NoticeViewModel {
        return viewModels<NoticeViewModel>().value
    }

    override fun getBind(): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val pagingData = it.data as PagingData<NoticeBeanItem>
                        noticeAdapter.submitData(pagingData)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.rvNotice.apply {
            adapter=noticeAdapter
            layoutManager= LinearLayoutManager(requireContext())
        }


        noticeAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(position: Int, item: NoticeBeanItem) {
                ToastUtils.showLong("${position}")
                Log.i("lr",item.toString())
            }
        })

        noticeAdapter.setOnItemLongClickListener(object : OnItemLongClickListener {
            override fun onItemLongClick(item: NoticeBeanItem): Boolean {
                ToastUtils.showLong("${item.toString()}")
                return true
            }
        })

        lifecycleScope.launch {
            vm.sendIntent(NoticeIntent.getNotices(0))
        }
    }


}